<!--
*Author：COCO
 *代码由框架生成,任何更改都可能导致被代码生成器覆盖
 *业务请在@/extension/custom/custom/Base_DefectItem.js此处编写
 -->
<template>
    <view-grid ref="grid"
               :columns="columns"
               :detail="detail"
               :editFormFields="editFormFields"
               :editFormOptions="editFormOptions"
               :searchFormFields="searchFormFields"
               :searchFormOptions="searchFormOptions"
               :table="table"
               :extend="extend">
    </view-grid>
    <ProductionOrderDetailDialog
    v-if="detailDialogVisible"
        v-model:visible="detailDialogVisible"
        :detail="currentDetailRow"
    />
</template>
<script>
    import ProductionOrderDetailDialog from './productmessage/ProductionOrderDetailDialog.vue';
    import extend from "@/extension/productmanage/productmanage/productorder.js";
    import { ref, defineComponent , getCurrentInstance, onMounted} from "vue";
    import http from '@/api/http'

    export default defineComponent({
        setup() {
            const detailDialogVisible = ref(false);
            const currentDetailRow = ref({});
            console.log('调用反填方法')
            async function showDetail(row) {
                console.log('调用反填方法入', row);
                // 确保数据是对象格式
                if (!row || typeof row !== 'object') {
                    console.error('Invalid row data:', row);
                    return;
                }
                // --- New: Fetch detail data from API ---
                let fetchedOrderProductList = [];
                let planninglist=[];
                if (row.ProductionOrderId) {
                    try {
                         await http.get(`/api/OrderProductList/GetOrderDetailsByIdAsync?productionOrderId=${row.ProductionOrderId}`, null, true)
                        .then(response => {
                            // 处理成功响应
                            fetchedOrderProductList= response;
                            console.log("成品信息",response);
                        })
                        .catch(error => {
                            // 处理错误
                            console.error(error);
                        });
                        await http.get(`/api/ProductionPlanning/GetOrderPlanningByIdAsync?productionOrderId=${row.ProductionOrderId}`, null, true)
                        .then(response => {
                            if(response.length>0){
                                planninglist= response;
                            }
                            else{
                               planninglist= [];
                            }
                            console.log("生产计划信息",response);
                        })
                        .catch(error => {
                            // 处理错误
                            console.error(error);
                        });
                    } catch (error) {
                        console.error('Error fetching detail data:', error);
                    }
                } else {
                    console.log('No ProductionOrderId found in row data, skipping API fetch.');
                }
                // 设置当前行数据
                currentDetailRow.value = { 
                    ProductionOrderId: row.ProductionOrderId || '',
                    ProductionOrderCode: row.ProductionOrderCode || '',
                    DCustomerId: row.DCustomerId || '',
                    OrderCode: row.OrderCode || '',
                    CreateDate: row.CreateDate || '',
                    Priority: row.Priority || '',
                    OrderSource: row.OrderSource || '',
                    Remarks: row.Remarks || '',
                    ProductList: row.ProductList || '',
                    ProductStatus:row.ProductStatus || '',
                    CreateUserName:row.CreateUserName    || '',
                    // Use the fetched detail data
                    OrderProductList: fetchedOrderProductList ,
                    PlanningList: planninglist,

                };
                console.log('设置 currentDetailRow.value:', currentDetailRow.value);
                detailDialogVisible.value = true;
                console.log('弹框已显示');
            }

            const table = ref({
                key: 'ProductionOrderId',
                footer: "Foots",
                cnName: '生产订单',
                name: 'productmanage/productmanage/ProductionOrderInfo',
                url: "/ProductionOrderInfo/",
                sortName: "CreateDate"
            });
            const editFormFields = ref({"ProductionOrderCode":"","DCustomerId":[],"OrderCode":"","CreateDate":"","Priority":false,"ProductStatus":[],"OrderSource":"","Remarks":"","ProductList":""});
            const editFormOptions = ref([[{"title":"生产编号","required":true,"field":"ProductionOrderCode"}],
                              [{"title":"客户","type":"select","dataKey":"ProductOrderCustomer","data":[],"required":true,"field":"DCustomerId"}],
                              [{"title":"订单编号","field":"OrderCode"}],
                              [{"title":"创建时间","required":true,"type":"date","field":"CreateDate"}],
                              [{"title":"优先级","field":"Priority","type":"radio","dataKey":"ProductPriority","data":[]}],
                              [{"title":"订单来源","required":true,"field":"OrderSource"}],
                              [{"title":"备注","required":true,"field":"Remarks"}],
                              [{"title":"生产状态","required":true,"type":"select","field":"ProductStatus","dataKey":"ProductState","data":[]}],
                              [{"title":"生产订单成品","required":true,"field":"ProductList"}]]);
             const searchFormFields = ref({"OrderCode":"","ProductStatus":[]});
             const searchFormOptions = ref([[{"title":"订单编号","field":"OrderCode","type":"like"},{"title":"生产状态","field":"ProductStatus","type":"select","dataKey": "ProductState","data":[]}]]);
            const columns = ref([{field:'ProductionOrderId',title:'生产订单主键',type:'guid',width:110,hidden:true,readonly:true,require:true,align:'left'},
                       {field:'ProductionOrderCode',title:'生产编号',type:'string',sort:true,width:180,align:'left'},
                       {field:'DCustomerId',title:'客户',type:'guid',sort:true,width:110,bind:{ key:'ProductOrderCustomer',data:[]},require:true,align:'left'},
                       {field:'OrderCode',title:'订单编号',type:'string',width:220,align:'left'},
                       {field:'CreateDate',title:'创建时间',type:'date',width:220,align:'left'},
                       {field:'Priority',title:'优先级',type:'int',width:110,align:'left'},
                       {field:'OrderSource',title:'订单来源',type:'int',width:80,hidden:true,align:'left'},
                       {field:'Remarks',title:'备注',type:'string',width:130,align:'left'},
                       {field:'ProductStatus',title:'生产状态',type:'int',bind:{ key:'ProductState',data:[]},width:130,align:'left'},
                       {field:'ProductList',title:'生产订单成品',type:'string',width:130,align:'left'}]);
            const detail = ref({
                cnName: "成品信息",
                table: "OrderProductList",
                columns: [
                    {field:'ProductId',title:'成品主键',type:'guid',width:110,hidden:true,readonly:true,require:true,align:'left', isSubmit: true },
                    {field:'ProductCode',title:'成品编号',type:'string',sort:true,width:110,require:true,align:'left',sort:true},
                    {field:'ProductName',title:'成品名称',type:'string',sort:true,width:180,edit:{type:'text'},require:true,align:'left'},
                    {field:'ProductType',title:'规格型号',type:'string',sort:true,width:180,edit:{type:'text'},require:true,align:'left'},
                    {field:'ProductBatch',title:'批次号',type:'string',sort:true,width:110,align:'left',sort:true},
                    {field:'ProductCompany',title:'单位',type:'int',width:80,hidden:true,align:'left'},
                    {field:'ProductNumS',title:'库存上限',type:'int',width:130,align:'left'},
                    {field:'ProductState',title:'成品状态',type:'string',width:130,align:'left'},
                    {field:'RawmanagementId',title:'原料编号',type:'guid',width:130,align:'left'},
                    {field:'RawmanagementBh',title:'原料名称',type:'string',width:130,align:'left'},
                    {field: 'SizeName', title: '尺寸名称',type: 'string', sort: true, width: 250, align: 'left', sort: true },
                    {field: 'DanWeiName', title: '单位名称',type: 'string', sort: true, width: 250, align: 'left', sort: true }
                ],
                sortName: "OrderProductListId",
                key: "OrderProductListId"
            });
            return {
                detailDialogVisible,
                currentDetailRow,
                showDetail,
                table,
                extend,
                editFormFields,
                editFormOptions,
                searchFormFields,
                searchFormOptions,
                columns,
                detail,
            };
        },
        components: {
            ProductionOrderDetailDialog,
        },
    });
    
</script>
